package com.btm.dorisinfo.Util;

import com.btm.dorisinfo.model.TableInfo;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TableInfoIndexBuilder {

    public static String buildTableInfoIndex(TableInfo tableInfo) throws JsonProcessingException {
        Map<String, Object> document = new HashMap<>();
        document.put("id", tableInfo.getId());
        document.put("tableSchema", tableInfo.getTableSchema());
        document.put("tableName", tableInfo.getTableName());
        document.put("tableComment", tableInfo.getTableComment());
        // 转换列信息为Map列表
        List<Map<String, Object>> columns = tableInfo.getColumns().stream()
                .map(column -> {
                    Map<String, Object> columnMap = new HashMap<>();
                    columnMap.put("columnName", column.getColumnName());
                    columnMap.put("dataType", column.getDataType());
                    columnMap.put("columnComment", column.getColumnComment());
                    columnMap.put("columnKey", column.getColumnKey());
                    columnMap.put("isNullable", column.getIsNullable());
                    columnMap.put("columnDefault", column.getColumnDefault());
                    return columnMap;
                })
                .collect(Collectors.toList());
        document.put("columns", columns);
        // 写入Meilisearch
        return new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(document);
    }

}
